package com.zhiwang.xiaoqu.ui.my

import android.graphics.Color
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.youth.banner.Banner
import com.youth.banner.BannerConfig
import com.zhiwang.xiaoqu.R
import com.zhiwang.xiaoqu.di.annotation.Injectable
import com.zhiwang.xiaoqu.di.component.DaggerUserComponent
import com.zhiwang.xiaoqu.di.module.AutoDisposeModule
import com.zhiwang.xiaoqu.ktextension.setImmersive
import com.zhiwang.xiaoqu.mvp.model.*
import com.zhiwang.xiaoqu.mvp.model.response.FriendTitle
import com.zhiwang.xiaoqu.mvp.presenter.UserPresenter
import com.zhiwang.xiaoqu.ui.BaseActivity
import com.zhiwang.xiaoqu.util.BannerImageLoader
import com.zhiwang.xiaoqu.widget.Toasty
import kotlinx.android.synthetic.main.activity_friend_list.*
import kotlinx.android.synthetic.main.item_friend.view.*
import javax.inject.Inject

class FriendListActivity : BaseActivity(),Injectable {

    val friendAdapter = FriendListAdapter()

    val datas = arrayListOf<FriendModel>()

    var page = 1

    @Inject
    lateinit var userPresenter: UserPresenter

    val bannerEnabled = true

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        window.setImmersive(Color.TRANSPARENT, true)
        setContentView(R.layout.activity_friend_list)
        setSupportActionBar(toolbar)
        recyclerView.adapter = friendAdapter

        onRefresh()

        smartRefreshLayout.setOnLoadMoreListener {
            onLoadMore()
        }
        smartRefreshLayout.setOnRefreshListener {
            onRefresh()
        }
    }

    private fun onRefresh() {
        userPresenter.getFriendInfo(::onRefreshData,::onError)
    }

    private fun onLoadMore() {
        userPresenter.getFriendList(page + 1, ::onLoadMoreData, ::onError)
    }

    fun onRefreshData(list: List<FriendModel>) {
        datas.clear()
        (list as ArrayList<FriendModel>).add(1, FriendTitle())
        datas.addAll(list)
        friendAdapter.notifyDataSetChanged()
        page = 1
        smartRefreshLayout.finishRefresh()
    }

    fun onLoadMoreData(list: List<FriendModel>) {
        datas.addAll(list)
        if (list.isNotEmpty())
            page++
        smartRefreshLayout.finishLoadMore()
    }


    inner class FriendListAdapter : RecyclerView.Adapter<BaseFriendViewHolder>() {

        override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseFriendViewHolder {
            return when (viewType) {
                TYPE_FRIEND_HEADER -> FriendHeaderViewHolder(
                    layoutInflater.inflate(
                        R.layout.item_friend_header,
                        parent,
                        false
                    )
                )
                TYPE_FRIEND_BANNER -> {
                    val holder =
                        FriendBannerViewHolder(layoutInflater.inflate(R.layout.item_friend_banner, parent, false))
                    holder.setIsRecyclable(false)
                    holder
                }
                TYPE_FRIEND_TITLE -> FriendListTitleViewHolder(
                    layoutInflater.inflate(
                        R.layout.item_friend_title,
                        parent,
                        false
                    )
                )
                TYPE_FRIEND_ITEM -> FriendListItemViewHolder(
                    layoutInflater.inflate(
                        R.layout.item_friend,
                        parent,
                        false
                    )
                )
                else -> FriendListTitleViewHolder(layoutInflater.inflate(R.layout.item_friend_title, parent, false))
            }
        }

        //0  头部； 1 banner ;2 title ;3 friend item
        override fun getItemViewType(position: Int): Int {
            return datas[position].type()
        }

        override fun getItemCount() = datas.size

        override fun onBindViewHolder(holder: BaseFriendViewHolder, position: Int) {
            holder.bind()
        }

    }

    abstract inner class BaseFriendViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        abstract fun bind()
    }

    inner class FriendHeaderViewHolder(itemView: View) : BaseFriendViewHolder(itemView) {

        val tvFriendNumber:TextView = itemView.findViewById(R.id.tv_friend_num)
        val tvValidFriendNumber:TextView = itemView.findViewById(R.id.tv_valid_friend_num)
        val tvFriendContribution :TextView = itemView.findViewById(R.id.tv_friend_contribution)

        override fun bind() {

            val friendModel = datas[adapterPosition]
            tvFriendNumber.text = friendModel.friendNumber()
            tvValidFriendNumber.text = friendModel.validFriendNumber()
            tvFriendContribution.text = friendModel.friendContributionSum()

        }

    }

    inner class FriendBannerViewHolder(itemView: View) : BaseFriendViewHolder(itemView) {

        override fun bind() {
            banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR)
                .setBannerTitles(arrayListOf("", "", ""))
                .setImages(
                    arrayListOf(
                        "https://images.gitee.com/uploads/images/2018/1226/113454_eec34b93_1305863.png",
                        "https://p0.meituan.net/dpdeal/c2b2181b7e393f8b065919fb684cf53c3920540.jpg%40450w_280h_1e_1c_1l%7Cwatermark%3D0",
                        "https://p1.meituan.net/dpdeal/8305c1c4bdb243cc5084204873e1e3a53508298.jpg%40450w_280h_1e_1c_1l%7Cwatermark%3D0"
                    )
                )
                .setImageLoader(BannerImageLoader)
                .setOnBannerListener { position ->
                    Toasty.message("clicked banner $position")
                }
                .isAutoPlay(true)
                .start()
        }

        val banner: Banner = itemView as Banner
    }

    inner class FriendListTitleViewHolder(itemView: View) : BaseFriendViewHolder(itemView) {

        override fun bind() {

        }
    }

    inner class FriendListItemViewHolder(itemView: View) : BaseFriendViewHolder(itemView) {

        val tvFriendUserName = itemView.tv_friend_username
        val tvFriendBenefit = itemView.tv_total_benefit
        val tvFriendContribution = itemView.tv_contribution

        override fun bind() {
            val model = datas[adapterPosition]
            tvFriendUserName.text = model.nickName()
            tvFriendBenefit.text = model.singleFriendBenefit()
            tvFriendContribution.text = model.singleFriendContribution()
        }

    }


    override fun onSupportNavigateUp(): Boolean {
        onBackPressed()
        return true
    }
}
